class_name RoadCreater

enum RoadType
{
	Horizontal,
	Vertical,
}

static func paint_road(tilemap:TileMapLayer,road_type:RoadType,road_info:RoadInfo,top_left_pos:Vector2i,bottom_right_pos:Vector2i,tile_callback:Callable):
	if road_type == RoadType.Horizontal:
		for y in range(top_left_pos.y,bottom_right_pos.y + 1):
			for x in range(top_left_pos.x,bottom_right_pos.x + 1):
				if x == top_left_pos.x:
					## TOP
					if y == top_left_pos.y:
						var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_left_top,1,false)[0]
						tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
						if tile_callback.is_valid():
							tile_callback.call(Vector2i(x,y))
					elif y == bottom_right_pos.y:
						var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_right_top,1,false)[0]
						tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
						if tile_callback.is_valid():
							tile_callback.call(Vector2i(x,y))
					else:
						var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_mid_top,1,false)[0]
						tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
						if tile_callback.is_valid():
							tile_callback.call(Vector2i(x,y))
				elif x == bottom_right_pos.x:
					## Bottom
					if y == top_left_pos.y:
						var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_left_bottom,1,false)[0]
						tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
						if tile_callback.is_valid():
							tile_callback.call(Vector2i(x,y))
					elif y == bottom_right_pos.y:
						var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_right_bottom,1,false)[0]
						tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
						if tile_callback.is_valid():
							tile_callback.call(Vector2i(x,y))
					else:
						var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_mid_bottom,1,false)[0]
						tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
						if tile_callback.is_valid():
							tile_callback.call(Vector2i(x,y))
				else:
					## Mid
					if y == top_left_pos.y:
						var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_left_mid,1,false)[0]
						tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
						if tile_callback.is_valid():
							tile_callback.call(Vector2i(x,y))
					elif y == bottom_right_pos.y:
						var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_right_mid,1,false)[0]
						tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
						if tile_callback.is_valid():
							tile_callback.call(Vector2i(x,y))
					else:
						var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_mid_mid,1,false)[0]
						tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
						if tile_callback.is_valid():
							tile_callback.call(Vector2i(x,y))
	elif road_type == RoadType.Vertical:
			for y in range(top_left_pos.y,bottom_right_pos.y + 1):
				for x in range(top_left_pos.x,bottom_right_pos.x - 1,-1):
					if x == top_left_pos.x:
						## TOP
						if y == top_left_pos.y:
							var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_left_top,1,false)[0]
							tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
							if tile_callback.is_valid():
								tile_callback.call(Vector2i(x,y))
						elif y == bottom_right_pos.y:
							var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_right_top,1,false)[0]
							tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
							if tile_callback.is_valid():
								tile_callback.call(Vector2i(x,y))
						else:
							var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_mid_top,1,false)[0]
							tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
							if tile_callback.is_valid():
								tile_callback.call(Vector2i(x,y))
					elif x == bottom_right_pos.x:
						## Bottom
						if y == top_left_pos.y:
							var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_left_bottom,1,false)[0]
							tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
							if tile_callback.is_valid():
								tile_callback.call(Vector2i(x,y))
						elif y == bottom_right_pos.y:
							var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_right_bottom,1,false)[0]
							tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
							if tile_callback.is_valid():
								tile_callback.call(Vector2i(x,y))
						else:
							var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_mid_bottom,1,false)[0]
							tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
							if tile_callback.is_valid():
								tile_callback.call(Vector2i(x,y))
					else:
						## Mid
						if y == top_left_pos.y:
							var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_left_mid,1,false)[0]
							tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
							if tile_callback.is_valid():
								tile_callback.call(Vector2i(x,y))
						elif y == bottom_right_pos.y:
							var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_right_mid,1,false)[0]
							tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
							if tile_callback.is_valid():
								tile_callback.call(Vector2i(x,y))
						else:
							var road_tile_info:RoadTileInfo = Utils.random_get_count(road_info.road_mid_mid,1,false)[0]
							tilemap.set_cell(Vector2i(x,y),road_tile_info.source_id,road_tile_info.atlas_coords,road_tile_info.alternative_tile)
							if tile_callback.is_valid():
								tile_callback.call(Vector2i(x,y))
